package com.sxs.dao;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;


@Repository
// 持久层
public class LoginDao implements LoginDaoInterface {
	Logger logger = Logger.getLogger(LoginDao.class);
	@Autowired
	private JdbcTemplate jdbcTemplatePer;
	private String[] str = {"btn btn-danger","btn btn-warning"};

	@Override
	public List<Map<String,Object>> getLoginMsg(String username, String password) {		
		String sql = "select  id,roleID,mobile from  vault_manager  where manger ='" 
	                + username + "' and pwd = '" + password + "' and islogin='1'";
		List<Map<String,Object>> list = jdbcTemplatePer.queryForList(sql);
		return list;
	}


	@Override
	public List<Map<String, Object>> getRightRole(String name) {
		String sql = "select rRole,headParameters,leftParameters,centerParameters from vault_right_role where id="
				+ name;
		List<Map<String, Object>> list = jdbcTemplatePer.queryForList(sql);
		return list;
	}



	@Override
	public List<Map<String, Object>> getMenu(String cid) {
		String sql = "select id,fid,menuName,type,sort,funParameters from per_menu where fid = 0 and isAccording = 1 order by sort";
		List<Map<String, Object>> list = jdbcTemplatePer.queryForList(sql);
		return list;
	}


	@Override
	public List<Map<String, Object>> getMenuFid(String fid) {
		String sqlVM = "select id,fid,menuName,type,sort,funParameters from per_menu where fid =" + fid
				+ " and isAccording = 1 order by sort";
		List<Map<String, Object>> listC = jdbcTemplatePer.queryForList(sqlVM);
		return listC;
	}


	@Override
	public String getVaultFunction(String cid, HttpServletRequest request, HttpSession session) {
		String sql = "select id,funName,link,funParameters,location from per_function where cid =" + cid;
		String returnStr = "";
		Map<String, List<String>> listSession = (Map<String, List<String>>) session.getAttribute("jurisdiction");
		List<String> listStr = listSession.get("center");
		List<Map<String, Object>> list = jdbcTemplatePer.queryForList(sql);
		int j = -1;
		for (int i = 0; i < list.size(); i++) {
			String idF = list.get(i).get("id") + "";
			if (listStr.contains(idF)) {
				j++;
				String funName =list.get(i).get("funName") + "";
				String link = list.get(i).get("link") + "";
				returnStr +="<td><input type=\"submit\" value=\""+funName+"\" class=\""+str[j]+"\" onclick=\"javascript:this.form.action='"+request.getContextPath()+link+"';\"></td>";
				if(i%3==0&&i!=0){
					j=-1;
				}
			
			}
		}

		return returnStr;
	}
	@Override
	public String getVaultFunctioBotton(String cid, HttpServletRequest request, HttpSession session) {
		String sql = "select id,funName,link,funParameters,location from per_function where cid =" + cid;
		String returnStr = "";
		Map<String, List<String>> listSession = (Map<String, List<String>>) session.getAttribute("jurisdiction");
		List<String> listStr = listSession.get("center");
		List<Map<String, Object>> list = jdbcTemplatePer.queryForList(sql);
		int j = -1;
		for (int i = 0; i < list.size(); i++) {
			String idF = list.get(i).get("id") + "";
			if (listStr.contains(idF)) {
				j++;
				String funName =list.get(i).get("funName") + "";
				String link = list.get(i).get("link") + "";
				returnStr +="<input type=\"button\" value=\""+funName+"\" class=\""+str[j]+"\" onclick=\""+link+";\">";
				if(i%3==0&&i!=0){
					j=-1;
				}
			
			}
		}
		return returnStr;
	}
	

	@Override
	public void getInsertUserActionLog(String userName, String remark) {
		String sql = "INSERT INTO user_action_log (userName,time,remark) VALUE ('"+userName+"',CURRENT_TIMESTAMP(),'"+remark+"')";
		jdbcTemplatePer.update(sql);
		
	}


	
}
